<html>
<head>
<title>BusinessData Services</title>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<link rel="stylesheet" type="text/css" href="services.css"/>
</head>
<body>

<h1>Search Certificates and Callsigns</h1>
<div class="hostInfo" style="display:none;">
<form action="">
<div>
<label for="scheme">Transport</label><select name="scheme" id="scheme">
<option value="http://">HTTP</option>
<option value="https://">HTTPS</option></select>
</div>
<div>
<label for="host">Host</label><select name="host" id="host"></select>
</div>
<div>
<label for="method">Method</label><select name="method" id="method">
<option value="GET">GET</option>
</select>
</div>
<div>
<label for="dialog">Force save dialog</label><input type="checkbox" id="forceDialog" name="forceDialog" checked="checked"/>
</div>
</div>

</form>
</div>
<div class="error"></div>
<div class="blurb"></div>
<div class="services">
<div class="serviceInfo">


<form id="actionform" target="_blank">
<div class="summary">Please enter ONLY ONE of the search criteria below and press <b>Submit</b></div>
<div>
<label for="clientId">Client ID:</label>
<input type="text" name="clientId" value="" class="field int" maxlength="6" />
</div>
<div>
<label for="clientName">Client Name:<sup>1</sup></label>
<input type="text" name="clientName" value="" class="field min2char alphaspace"  maxlength="50"/><span class="hint">Minimum 2 characters</span>
</div>
<div>
<label for="callsign">Callsign:<sup>1</sup></label>
<input type="text" name="callsign" value="" class="field min2char alpha" maxlength="7"/><span class="hint">Minimum 2 characters</span>
</div>
<div>
<label for="certificateNumber">Certificate Number:<sup>1</sup></label>
<input type="text" name="certificateNumber" value="" class="field min2char alphaspace" maxlength="12"/><span class="hint">Minimum 2 characters</span>
</div>
<div>
<label for="vesselName">Vessel Name:<sup>1</sup></label>
<input type="text" name="vesselName" value="" class="field alphaspace" maxlength="100"/><span class="hint">Minimum 2 characters</span>
</div>
<div>
<label for="mmsi">MMSI:</label>
<input type="text" name="mmsi" value="" class="field int" maxlength="10"/>
</div>
<div>
<label for="rtn">RTN:</label>
<input type="text" name="rtn" value="" class="field int" maxlength="10"/>
</div>
<div class="summary"><sup>1</sup>You can enter "%" after the text you want to search for as a substitute for zero or more characters. e.g, Z% will search for all values starting with "Z".</div>
<button onclick="dosubmit(this.form);return false;">Submit</button>
<button type="reset">Reset</button>
</form>
</div>
</div>

<script type="text/javascript">
var topWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                   .getInterface(Components.interfaces.nsIWebNavigation)
                   .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
                   .rootTreeItem
                   .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                   .getInterface(Components.interfaces.nsIDOMWindow);
var BusinessDataChrome = topWindow.BusinessDataChrome; 
var baseUrl = '/data/app/ws/rest/rsm/certificatesAndCallsigns/search/v1.0';
function dosubmit(frm){
   hideError();
   var $host = $('#host option:selected');
   var $scheme = $('#scheme option:selected');
   var $method = $('#method option:selected');
   var url = baseUrl;
   if ($host.val() == 'localhost'){
        url = url.replace(/\/data\/app\/ws/, ':8080/fmit-g2b-webservice');
   }
   frm.method = $method.val();
   frm.action = $scheme.val()+$host.val()+url;
   var inp = $('.field[value!=""]');
   if (inp.length > 1){
     alert('nice try');
   } else if (inp.length == 0){
     showError('Please specify your query');  
   } else {
     var fld = $(inp[0]);
     if (fld.hasClass('min2char') && fld.val().length < 2){
        showError('Please specify at least 2 characters for a search on the '+fieldLabel(fld));
     } else if (fld.hasClass('int') && !fld.val().match(/^\d+$/)){
         showError('Not a valid '+fieldLabel(fld)+'.  Please re-enter a number greater than zero');
     } else if (fld.hasClass('alpha') && !fld.val().match(/^([\d%\w])+$/)){
         showError('Not a valid '+fieldLabel(fld)+'.  Please re-enter an alphanumeric value');
     } else if (fld.hasClass('alphaspace') && !fld.val().match(/^([\d% \w])+$/)){
         showError('Not a valid '+fieldLabel(fld)+'.  Please re-enter an alphanumeric value');
     } else if ((fld.hasClass('alpha') || fld.hasClass('alphaspace')) && $.trim(fld.val()) == '%%'){
         showError('You cannot enter "%%". Please specify new search criteria');    
         
     } else {
        frm.submit();
     }
      
   }
   
}

function showError(mes){
        $('.error').html('<div>'+mes+'</div>');
        $('.error').slideDown();
}
function fieldLabel(fld){
    var nme = fld.attr('name');
    var lbl = $('label[for='+nme+']');
    
    return lbl.text().substring(0, lbl.text().indexOf(':'));
}

function hideError(){
    $('.error').slideUp();
}
$(function(){
  BusinessDataChrome.initAccessInfo();
  BusinessDataChrome.forceSaveDialog = true;
  var sel = $('#host').get(0);
  for(key in BusinessDataChrome.accessInfo){
    var op = new Option(key, key,false);
    sel.options.add(op);
  }
  $('#forceDialog').change(function(){
    BusinessDataChrome.forceSaveDialog = $('#forceDialog:checked').length >0
    }
  );
  if (sel.options.length > 1){
    $('.hostInfo').show();
  }
  $('.blurb').text('Simple GET request to '+baseUrl);
  $('.field').change(function(){
    var fld = this;
    $('.field').each(function(){
        if (fld != this){
            $(this).val('');  
            hideError();      
        }
    });  
  });
});
</script>
</body>
</html>
